package com.gable.exhibition.service.impl;

import com.gable.common.util.ChineseToEnglish;
import com.gable.common.util.MD5Util;
import com.gable.common.util.ReturnResultUtil;
import com.gable.common.util.UUIDGenerator;
import com.gable.exhibition.dao.*;
import com.gable.exhibition.init.MaintainerManufacturerInit;
import com.gable.exhibition.init.ManufacturerInit;
import com.gable.exhibition.init.ManufacturerSupplierInit;
import com.gable.exhibition.init.SupplierManufacturerInit;
import com.gable.exhibition.model.*;
import com.gable.exhibition.service.ManufacturerService;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.ImportExcelManufacturerModel;
import com.gable.exhibition.view.MaintainerView;
import com.gable.exhibition.view.ManufacturerView;
import com.gable.exhibition.view.SupplierView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Lesson on 2018/2/9.
 */
@Service("manufacturerService")
public class ManufacturerServiceImpl implements ManufacturerService {

    @Autowired
    ManufacturerDao manufacturerDao;

    @Autowired
    UserDao userDao;

    @Autowired
    ManufacturerMaintainerDao manufacturerMaintainerDao;

    @Autowired
    MaintainerManufacturerInit maintainerManufacturerInit;

    @Autowired
    MaintainerDao maintainerDao;

    @Autowired
    ManufacturerInit manufacturerInit;

    @Autowired
    SupplierDao supplierDao;

    @Autowired
    ManufacturerSupplierDao manufacturerSupplierDao;

    @Autowired
    ManufacturerSupplierInit manufacturerSupplierInit;

    @Autowired
    SupplierManufacturerInit supplierManufacturerInit;

    @Override
    public ReturnResultUtil queryManufacturerList(ManufacturerView manufacturerView) {
        int count = manufacturerDao.queryManufacturerCount(manufacturerView);
        List<ManufacturerView> list = manufacturerDao.queryManufacturerList(manufacturerView);
        ManufacturerView manufacturerView1 = new ManufacturerView();
        manufacturerView1.setCount(count);
        manufacturerView1.setList(list);
        return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,manufacturerView1);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ReturnResultUtil addOrUpdateManufacturer(ManufacturerView manufacturerView) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int count = manufacturerDao.checkManufacturerNo(manufacturerView);
        if(count > 0){
            return new ReturnResultUtil(500,MessageUtil.MANUDOESTEXIST);
        }
        int result = 0;
        //插入厂家维修商，维修商中包含厂家自己
        Maintainer maintainer = new Maintainer();
        if(manufacturerView.getManufacturerId() == null){
            manufacturerView.setManufacturerId(UUIDGenerator.getUniqueLong());
            result = manufacturerDao.insertSelective(manufacturerView);
            if(result > 0){
                maintainer.setMaintainerId(UUIDGenerator.getUniqueLong());
                maintainer.setIsDel(0);
                maintainer.setServerPhone(manufacturerView.getServerPhone());
                maintainer.setRemark(manufacturerView.getRemark());
                maintainer.setRegistrationNumber(manufacturerView.getRegistrationNumber());
                maintainer.setAddress(manufacturerView.getAddress());
                maintainer.setAreaId(manufacturerView.getAreaId());
                maintainer.setCityId(manufacturerView.getCityId());
                maintainer.setProvinceId(manufacturerView.getProvinceId());
                maintainer.setCreatePerson(manufacturerView.getCreatePerson());
                maintainer.setCreateTime(manufacturerView.getCreateTime());
                maintainer.setCreditCode(manufacturerView.getCreditCode());
                maintainer.setMaintainerContacts(manufacturerView.getManufacturerContacts());
                maintainer.setMaintainerName(manufacturerView.getManufacturerName());
                maintainer.setMaintainerPhone(manufacturerView.getManufacturerPhone());
                maintainer.setManufacturer(true);
                maintainerDao.insertSelective(maintainer);

                //插入厂商和维修商之间的关系
                ManufacturerMaintainer manufacturerMaintainer = new ManufacturerMaintainer();
                manufacturerMaintainer.setManufacturerMaintainerId(UUIDGenerator.getUniqueLong());
                manufacturerMaintainer.setManufacturerId(manufacturerView.getManufacturerId());
                manufacturerMaintainer.setMaintainerId(maintainer.getMaintainerId());
                manufacturerMaintainer.setOrderBy(-1);
                manufacturerMaintainerDao.insertSelective(manufacturerMaintainer);

                //初始化厂家管理员
                User users = new User();
                users.setUserId(UUIDGenerator.getUniqueLong());
                users.setTag(1003L);
                users.setType(1102L);
                String userName = ChineseToEnglish.getPinYinHeadChar(manufacturerView.getManufacturerName())+"_admin";
                users.setUserName(userName);
                String password = MD5Util.md5("123456");
                users.setPassword(password);
                users.setRealName("厂商管理员");
                users.setManufacturerId(manufacturerView.getManufacturerId());
                users.setRemark("默认生成厂商管理员");
                users.setCreateTime(df.format(new Date()));
                users.setCreatePerson(manufacturerView.getCreatePerson());
                users.setIsDel(0);
                userDao.insertSelective(users);
            }
        }else{
            result = manufacturerDao.updateByPrimaryKeySelective(manufacturerView);
        }
        if(result>0){
            return new ReturnResultUtil(200,MessageUtil.SAVESUCCESS,maintainer);
        }
        return new ReturnResultUtil(500,MessageUtil.SAVEFAIL);
    }

    @Override
    public ReturnResultUtil queryManufacturerInfo(Long manufacturerId) {
        ManufacturerView manufacturerView = manufacturerDao.queryManufacturerInfo(manufacturerId);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,manufacturerView);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ReturnResultUtil saveManufacturerByExcel(List<ImportExcelManufacturerModel> list, User user) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int count = 0;
        for(ImportExcelManufacturerModel im : list){
            Manufacturer manufacturer = new Manufacturer();

            manufacturer.setManufacturerId(UUIDGenerator.getUniqueLong());
            manufacturer.setCreatePerson(user.getUserId());
            manufacturer.setManufacturerName(im.getManufacturerName());
            manufacturer.setManufacturerNo(im.getManufacturerNo());
            manufacturer.setAddress(im.getAddress());
            manufacturer.setAreaId(im.getAreaId());
            manufacturer.setCreateTime(df.format(new Date()));
            manufacturer.setCityId(im.getCityId());
            manufacturer.setProvinceId(im.getProvinceId());
            manufacturer.setCreditCode(im.getCreditCode());
            manufacturer.setRegistrationNumber(im.getRegistrationNumber());
            manufacturer.setIsDel(0);
            manufacturer.setManufacturerContacts(im.getManufacturerContacts());
            manufacturer.setManufacturerPhone(im.getManufacturerPhone());
            manufacturer.setServerPhone(im.getServerPhone());
            manufacturer.setRemark(im.getRemark());
            manufacturer.setShortName(im.getShortName());

            int result = manufacturerDao.insertSelective(manufacturer);
            if(result>0){

                //插入厂家维修商，维修商中包含厂家自己
                Maintainer maintainer = new Maintainer();
                maintainer.setMaintainerId(UUIDGenerator.getUniqueLong());
                maintainer.setIsDel(0);
                maintainer.setServerPhone(manufacturer.getServerPhone());
                maintainer.setRemark(manufacturer.getRemark());
                maintainer.setRegistrationNumber(manufacturer.getRegistrationNumber());
                maintainer.setAddress(manufacturer.getAddress());
                maintainer.setAreaId(manufacturer.getAreaId());
                maintainer.setCityId(manufacturer.getCityId());
                maintainer.setProvinceId(manufacturer.getProvinceId());
                maintainer.setCreatePerson(manufacturer.getCreatePerson());
                maintainer.setCreateTime(manufacturer.getCreateTime());
                maintainer.setCreditCode(manufacturer.getCreditCode());
                maintainer.setMaintainerContacts(manufacturer.getManufacturerContacts());
                maintainer.setMaintainerName(manufacturer.getManufacturerName());
                maintainer.setMaintainerPhone(manufacturer.getManufacturerPhone());
                maintainer.setShortName(manufacturer.getShortName());
                maintainer.setManufacturer(true);
                maintainerDao.insertSelective(maintainer);

                //插入厂商和维修商之间的关系
                ManufacturerMaintainer manufacturerMaintainer = new ManufacturerMaintainer();
                manufacturerMaintainer.setManufacturerMaintainerId(UUIDGenerator.getUniqueLong());
                manufacturerMaintainer.setManufacturerId(manufacturer.getManufacturerId());
                manufacturerMaintainer.setMaintainerId(maintainer.getMaintainerId());
                manufacturerMaintainer.setOrderBy(-1);
                manufacturerMaintainerDao.insertSelective(manufacturerMaintainer);

                //初始化厂商管理员
                User users = new User();
                users.setUserId(UUIDGenerator.getUniqueLong());
                users.setTag(1003L);
                users.setType(1102L);
                String userName = ChineseToEnglish.getPinYinHeadChar(manufacturer.getManufacturerName())+"_admin";
                users.setUserName(userName);
                String password = MD5Util.md5("123456");
                users.setPassword(password);
                users.setRealName("厂商管理员");
                users.setManufacturerId(manufacturer.getManufacturerId());
                users.setRemark("默认生成厂商管理员");
                users.setCreateTime(df.format(new Date()));
                users.setCreatePerson(manufacturer.getCreatePerson());
                users.setIsDel(0);
                userDao.insertSelective(users);
                count ++ ;
            }
        }
        if(count != list.size()){
            ImportExcelManufacturerModel m=null;
            m.getAddress();
        }
        return new ReturnResultUtil(200,MessageUtil.SAVESUCCESS);
    }

    @Override
    public ReturnResultUtil queryManufacturerListBymainId(Long maintainerId) {
        List<ManufacturerView> list = manufacturerDao.queryManufacturerListBymainId(maintainerId);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    public ReturnResultUtil bindMaintainerInit(MaintainerView maintainerView) {
        List<MaintainerView> list = maintainerDao.bindMaintainerInit(maintainerView);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ReturnResultUtil saveSetMaintainer(List<Long> maintainerIdList, Long manufacturerId) {
            manufacturerMaintainerDao.deleteBaseByManufacturerId(manufacturerId);
            for(Long maintainerId : maintainerIdList){
                ManufacturerMaintainer mm = new ManufacturerMaintainer();
                mm.setManufacturerMaintainerId(UUIDGenerator.getUniqueLong());
                mm.setMaintainerId(maintainerId);
                mm.setManufacturerId(manufacturerId);
                int result = manufacturerMaintainerDao.insertSelective(mm);
                if(result > 0){

                }
            }
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        manufacturerInit.updateManufacturerMaintainer();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();

            Thread ts = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        maintainerManufacturerInit.queryMaintainerManufacturer();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            ts.start();
        return new ReturnResultUtil(200,MessageUtil.SAVESUCCESS);
    }

    @Override
    public ReturnResultUtil queryManufacturerListBySuppId(Long supplierId) {
        List<ManufacturerView> list = manufacturerDao.queryManufacturerListBySuppId(supplierId);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    public ReturnResultUtil bindSupplierInit(SupplierView supplierView) {
        List<SupplierView> list = supplierDao.bindSupplierInit(supplierView);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,list);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ReturnResultUtil saveSetSupplier(List<Long> supplierIdList, Long manufacturerId) {
        manufacturerSupplierDao.deleteBaseByManufacturerId(manufacturerId);
        for(Long supplierId : supplierIdList){
            ManufacturerSupplier ms = new ManufacturerSupplier();
            ms.setManufacturerSupplierId(UUIDGenerator.getUniqueLong());
            ms.setSupplierId(supplierId);
            ms.setManufacturerId(manufacturerId);
            int result = manufacturerSupplierDao.insertSelective(ms);
            if(result > 0){

            }
        }
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    manufacturerSupplierInit.queryManufacturerSupplier();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();

        Thread ts = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    supplierManufacturerInit.querySupplierManufacturer();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        ts.start();

        return new ReturnResultUtil(200,MessageUtil.SAVESUCCESS);
    }

}
